package com.xc.result;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serializable;

/**
 * 统一响应结果集resultFul风格
 */
@Data
public class ResultVO<T> implements Serializable {
    @Schema(title = "响应码", description = "200代表成功，其余代表失败")
    private Integer code;
    @Schema(title = "提示信息", description = "执行信息")
    private String msg;
    @Schema(title = "数据", description = "返回的数据")
    private T data;

    // 成功，不返回数据
    public static <T> ResultVO<T> success() {
        ResultVO<T> resultVO = new ResultVO<>();
        resultVO.setCode(200);
        resultVO.setMsg("成功");
        return resultVO;
    }

    // 成功，返回数据
    public static <T> ResultVO<T> success(T data) {
        ResultVO<T> resultVO = new ResultVO<>();
        resultVO.setCode(200);
        resultVO.setMsg("成功");
        resultVO.setData(data);
        return resultVO;
    }

    // 失败，带code,data
    public static <T> ResultVO<T> error(Integer errorCode, String data) {
        ResultVO<T> resultVO = new ResultVO<>();
        resultVO.setCode(errorCode);
        resultVO.setMsg("失败");
        resultVO.setData((T) data);
        return resultVO;
    }

    // 失败，带data
    public static <T> ResultVO<T> error(String data) {
        ResultVO<T> resultVO = new ResultVO<>();
        resultVO.setCode(0);
        resultVO.setMsg("失败");
        resultVO.setData((T) data);
        return resultVO;
    }
}
